package com.atguigu.system.service.impl;

import com.atguigu.common.helper.MenuHelper;
import com.atguigu.common.result.ResultCodeEnum;
import com.atguigu.model.system.SysMenu;
import com.atguigu.model.system.SysRoleMenu;
import com.atguigu.model.vo.AssginMenuVo;
import com.atguigu.system.exception.GuiguException;
import com.atguigu.system.mapper.SysMenuMapper;
import com.atguigu.system.mapper.SysRoleMenuMapper;
import com.atguigu.system.service.SysMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> findMenuTree() {
        //调用SysMenuMapper中获取所有的方法
        List<SysMenu> sysMenus = sysMenuMapper.selectList(null);
        //通过MenuHelper工具类将所有的菜单转换为菜单树
        List<SysMenu> menuTree = MenuHelper.buildTree(sysMenus);
        return menuTree;
    }

    @Override
    public void deleteNodeById(Long id) {
        //判断当前节点是否有子节点
        Integer count = sysMenuMapper.selectCount(new QueryWrapper<SysMenu>().eq("parent_id", id));
        if(count > 0){
            //证明该节点是父节点，不能删除，抛出异常
            throw new GuiguException(ResultCodeEnum.NODE_ERROR);
        }else{
            //非父节点，根据id删除该节点
            sysMenuMapper.deleteById(id);
        }

    }

    @Override
    public List<SysMenu> getRoleMenuList(Long roleId) {
        //获取所有的权限菜单
        List<SysMenu> sysMenuList = sysMenuMapper.selectList(new QueryWrapper<SysMenu>().eq("status", 1));

        //根据角色id查询中间表获取SysRoleMenu对象
        List<SysRoleMenu> roleMenuList = sysRoleMenuMapper.selectList(new QueryWrapper<SysRoleMenu>().eq("role_id",roleId));

        //创建一个保存角色已分配权限菜单id的集合
        List<Long> roleMenuIds = new ArrayList<>();
        //遍历roleMenuList
        for (SysRoleMenu sysRoleMenu : roleMenuList) {
            //将所有选中的权限菜单的id放到roleMenuIds中
            roleMenuIds.add(sysRoleMenu.getMenuId());
        }

        //遍历所有的权限菜单
        for (SysMenu sysMenu : sysMenuList) {
            //判断roleMenuIds集合中是否包含当前权限菜单的id
            if(roleMenuIds.contains(sysMenu.getId())){
                //设置该权限菜单被选中
                sysMenu.setSelect(true);
            }
        }

        //将权限菜单转换为菜单树
        List<SysMenu> sysMenuTree = MenuHelper.buildTree(sysMenuList);
        return sysMenuTree;
    }

    @Override
    public void assignMenu(AssginMenuVo assginMenuVo) {
        //获取角色id
        Long roleId = assginMenuVo.getRoleId();
        //根据角色id删除之前分配的权限菜单
        sysRoleMenuMapper.delete(new QueryWrapper<SysRoleMenu>().eq("role_Id",roleId));
        //获取现在分配的权限菜单的id
        List<Long> menuIdList = assginMenuVo.getMenuIdList();
        //遍历
        for (Long aLong : menuIdList) {
            //创建SysRoleMenu对象
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            //设置角色id
            sysRoleMenu.setRoleId(roleId);
            //设置菜单id
            sysRoleMenu.setMenuId(aLong);
            //调用SysRoleMenuMapper中插入的方法
            sysRoleMenuMapper.insert(sysRoleMenu);
        }
    }
}
